<?php

class LoginController extends Controller {

    public function actionIndex() {
        $this->layout = '//layouts/login';

        $model = new login_form;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['login_form'])) {
            $model->attributes = $_POST['login_form'];

            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect('main/dashboard');
        }
        // display the login form
        //$this->render('index', array('model' => $model));
        $this->renderPartial('login', array('model' => $model));
    }

    public function actionAuto() {
        
        //if (isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] == Yii::app()->getBaseUrl(true).'/admin/system/member/list')) {
        //if(isset($_SERVER['HTTP_REFERER']) && (preg_match('/' . $_SERVER['HTTP_REFERER'] . '$/', Yii::app()->getBaseUrl(true).'/admin/system/member/list(.*)'))) {
        //if(isset($_SERVER['HTTP_REFERER']) && (preg_match('/' . Yii::app()->getBaseUrl(true).'/admin/system/member/list(.*)' . '$/', $_SERVER['HTTP_REFERER']))) {  
         if(preg_match('/' . str_replace('/', '\/', Yii::app()->getBaseUrl(true).'/admin/system/member/list') . '(.*)' . '$/', $_SERVER['HTTP_REFERER'])) {
        
        $username = function_lib::uri_segment(4);
            $identity = new MemberAutoIdentity($username, '');
            if ($identity->authenticate()) {
                Yii::app()->user->login($identity);
                Yii::app()->controller->redirect(array('/member/main/information'));
            }
        } else {
            die('woyoyyy');
            Yii::app()->controller->redirect(array('/login'));
        }
    }

}